package gew.beans;

import gew.data.DimensionValue;
import gew.metadata.Queries;
import gew.system.Constants;
import gew.system.Labels;
import gew.system.Parameters;
import gew.system.RequestParameter;
import gew.utils.SqlUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

@ManagedBean(name = "trdBn")
@RequestScoped
public class TradeBean2 extends BaseBean{

    private RequestParameter rp;
    private List<DimensionValue> reporters;
    private List<DimensionValue> tradeFlows;
    private List<DimensionValue> partners;
    private List<DimensionValue> periods;
    private List<DimensionValue> classifications;
    private List<DimensionValue> commodities;

    private String urlTable;

    private String inclTreeStart;
    private String urlTreeStart;

    private String urlTreeReporter;
    private String inclTreeReporterInit;
    private String inclTreeReporterChildren;
    private String inclTreeReporterSearch;

    private String urlTreeTradeFlow;
    private String inclTreeTradeFlowInit;
    private String inclTreeTradeFlowChildren;
    private String inclTreeTradeFlowSearch;

    private String urlTreePartner;
    private String inclTreePartnerInit;
    private String inclTreePartnerChildren;
    private String inclTreePartnerSearch;

    private String urlTreePeriod;
    private String inclTreePeriodInit;
    private String inclTreePeriodChildren;
    private String inclTreePeriodSearch;

    private String urlTreeClassification;
    private String inclTreeClassificationInit;
    private String inclTreeClassificationChildren;
    private String inclTreeClassificationSearch;

    private String urlTreeCommodity;
    private String inclTreeCommodityInit;
    private String inclTreeCommodityChildren;
    private String inclTreeCommoditySearch;

    private String top;
    private String partnersGenetivus;
    private ArrayList<Integer> topPartners;
    private int topPartnersRank;

    private String reporterNames;
    private String reporterNamesGen;
    private String reporterNamesAcc;
    private String partnerNames;
    private String partnerNamesGen;
    private String partnerNamesAcc;
    private String periodNames;
    private String classificationNames;
    private String commodityNames;

    @PostConstruct
    public void init() {
        HttpServletRequest r = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        rp = new RequestParameter(r, dvs);
        logRequest(r, Constants.SECTION_TRADE, rp.getRequestURI());
        
        reporters = dvs.getSelectedValues(rp.getReporterIds(), rp.getLocale(), Queries.CountryDimensionValue);
        tradeFlows = dvs.getSelectedValues(rp.getTradeFlowIds(), rp.getLocale(), Queries.TradeFlowDimensionValue);
        partners = dvs.getSelectedValues(rp.getPartnerIds(), rp.getLocale(), Queries.CountryDimensionValue);
        periods = dvs.getSelectedValues(rp.getPeriodIds(), rp.getLocale(), Queries.PeriodDimensionValue);
        classifications = dvs.getSelectedValues(rp.getCommodityGroupTypeIds(), rp.getLocale(), Queries.CommodityGroupTypeDimensionValue);
        commodities = dvs.getSelectedValues(rp.getCommodityIds(), rp.getLocale(), Queries.CommodityDimensionValue);

        reporterNames = dvs.getAllNames(Queries.CountryDimensionValue, rp.getReporterIds(), rp.getLocale());
        reporterNamesGen = dvs.getAllNamesGen(Queries.CountryDimensionValue, rp.getReporterIds(), rp.getLocale());
        reporterNamesAcc = dvs.getAllNamesAcc(Queries.CountryDimensionValue, rp.getReporterIds(), rp.getLocale());
        partnerNames = dvs.getAllNames(Queries.CountryDimensionValue, rp.getPartnerIds(), rp.getLocale());
        partnerNamesGen = dvs.getAllNamesGen(Queries.CountryDimensionValue, rp.getPartnerIds(), rp.getLocale());
        partnerNamesAcc = dvs.getAllNamesAcc(Queries.CountryDimensionValue, rp.getPartnerIds(), rp.getLocale());
        periodNames = dvs.getAllNames(Queries.PeriodDimensionValue, rp.getPeriodIds(), rp.getLocale());
        classificationNames = dvs.getAllNames(Queries.CommodityGroupTypeDimensionValue, rp.getCommodityGroupTypeIds(), rp.getLocale());
        commodityNames = dvs.getAllCodesAndNames(Queries.CommodityDimensionValue, rp.getCommodityIds(), rp.getLocale());

        // <editor-fold defaultstate="collapsed" desc="Trees' urls">
        urlTreeStart = rp.getContextPath()
                .concat(SqlUtils.VERTICAL_BAR)
                .concat(rp.getLocaleUrlCode())
                .concat(SqlUtils.VERTICAL_BAR)
                .concat(Constants.SECTION_TRADE)
                .concat(SqlUtils.VERTICAL_BAR);

        inclTreeStart = rp.getHttpHeader()
                .concat(SqlUtils.SLASH)
                .concat(rp.getLocaleUrlCode())
                .concat(SqlUtils.SLASH)
                .concat(Constants.SECTION_TREE)
                .concat(SqlUtils.SLASH);

        // <editor-fold defaultstate="collapsed" desc="Reporter">
        urlTreeReporter = urlTreeStart
                .concat(SqlUtils.AT)
                //.concat(rp.getReporterUrlCode())
                .concat(SqlUtils.EMPTY)
                .concat(SqlUtils.AT)
                .concat(SqlUtils.VERTICAL_BAR)
                .concat(rp.getTradeFlowUrlCode())
                .concat(SqlUtils.VERTICAL_BAR)
                .concat(rp.getPartnerUrlCode())
                .concat(SqlUtils.VERTICAL_BAR)
                .concat(rp.getPeriodUrlCode())
                .concat(SqlUtils.VERTICAL_BAR)
                .concat(rp.getCommodityGroupTypeUrlCode())
                .concat(SqlUtils.VERTICAL_BAR)
                .concat(rp.getCommodityUrlCode())
                .concat(SqlUtils.VERTICAL_BAR);

        String hierGroupReporter = (new Integer(Constants.HIER_GROUP_REPORTER)).toString();
        inclTreeReporterInit = inclTreeStart
                .concat(Constants.FILTER_REQUEST_INIT)
                .concat(SqlUtils.SLASH)
                .concat(Constants.TREE_TEMPLATE_NO_SPACES)
                .concat(SqlUtils.SLASH)
                .concat(hierGroupReporter)
                .concat(SqlUtils.SLASH)
                .concat(urlTreeReporter)
                .concat(SqlUtils.SLASH)
                .concat(rp.getReporterIdsHS())
                //.concat(SqlUtils.EMPTY)
                .concat(SqlUtils.SLASH);
        inclTreeReporterChildren = inclTreeStart
                .concat(Constants.FILTER_REQUEST_CHILDREN)
                .concat(SqlUtils.SLASH)
                .concat(Constants.TREE_TEMPLATE_NO_SPACES)
                .concat(SqlUtils.SLASH)
                .concat(hierGroupReporter)
                .concat(SqlUtils.SLASH)
                .concat(urlTreeReporter)
                .concat(SqlUtils.SLASH)
                .concat(Constants.TREE_VAR_PARENT_ID)
                .concat(SqlUtils.SLASH);
        inclTreeReporterSearch = inclTreeStart
                .concat(Constants.FILTER_REQUEST_SEARCH)
                .concat(SqlUtils.SLASH)
                .concat(Constants.TREE_TEMPLATE_NO_SPACES)
                .concat(SqlUtils.SLASH)
                .concat(hierGroupReporter)
                .concat(SqlUtils.SLASH)
                .concat(urlTreeReporter)
                .concat(SqlUtils.SLASH)
                .concat(rp.getReporterIdsHS())
                //.concat(SqlUtils.EMPTY)
                .concat(SqlUtils.SLASH)
                .concat(Constants.TREE_VAR_SEARCH_TEXT)
                .concat(SqlUtils.SLASH);

        // </editor-fold>
        // <editor-fold defaultstate="collapsed" desc="Trade Flow">
        urlTreeTradeFlow = urlTreeStart
                .concat(rp.getReporterUrlCode())
                .concat(SqlUtils.VERTICAL_BAR)
                .concat(SqlUtils.AT)
                .concat(rp.getTradeFlowUrlCode())
                .concat(SqlUtils.AT)
                .concat(SqlUtils.VERTICAL_BAR)
                .concat(rp.getPartnerUrlCode())
                .concat(SqlUtils.VERTICAL_BAR)
                .concat(rp.getPeriodUrlCode())
                .concat(SqlUtils.VERTICAL_BAR)
                .concat(rp.getCommodityGroupTypeUrlCode())
                .concat(SqlUtils.VERTICAL_BAR)
                .concat(rp.getCommodityUrlCode())
                .concat(SqlUtils.VERTICAL_BAR);

        String hierGroupTradeFlow = (new Integer(Constants.HIER_GROUP_TRADE_FLOW)).toString();
        inclTreeTradeFlowInit = inclTreeStart
                .concat(Constants.FILTER_REQUEST_INIT)
                .concat(SqlUtils.SLASH)
                .concat(Constants.TREE_TEMPLATE_NO_SPACES_NO_CODES_BOLD1)
                .concat(SqlUtils.SLASH)
                .concat(hierGroupTradeFlow)
                .concat(SqlUtils.SLASH)
                .concat(urlTreeTradeFlow)
                .concat(SqlUtils.SLASH)
                .concat(rp.getTradeFlowIdsHS())
                .concat(SqlUtils.SLASH);
        inclTreeTradeFlowChildren = inclTreeStart
                .concat(Constants.FILTER_REQUEST_CHILDREN)
                .concat(SqlUtils.SLASH)
                .concat(Constants.TREE_TEMPLATE_NO_SPACES_NO_CODES_BOLD1)
                .concat(SqlUtils.SLASH)
                .concat(hierGroupTradeFlow)
                .concat(SqlUtils.SLASH)
                .concat(urlTreeTradeFlow)
                .concat(SqlUtils.SLASH)
                .concat(Constants.TREE_VAR_PARENT_ID)
                .concat(SqlUtils.SLASH);
        inclTreeTradeFlowSearch = inclTreeStart
                .concat(Constants.FILTER_REQUEST_SEARCH)
                .concat(SqlUtils.SLASH)
                .concat(Constants.TREE_TEMPLATE_NO_SPACES_NO_CODES_BOLD1)
                .concat(SqlUtils.SLASH)
                .concat(hierGroupTradeFlow)
                .concat(SqlUtils.SLASH)
                .concat(urlTreeTradeFlow)
                .concat(SqlUtils.SLASH)
                .concat(rp.getTradeFlowIdsHS())
                .concat(SqlUtils.SLASH)
                .concat(Constants.TREE_VAR_SEARCH_TEXT)
                .concat(SqlUtils.SLASH);

        // </editor-fold>
        // <editor-fold defaultstate="collapsed" desc="Partner">
        urlTreePartner = urlTreeStart
                .concat(rp.getReporterUrlCode())
                .concat(SqlUtils.VERTICAL_BAR)
                .concat(rp.getTradeFlowUrlCode())
                .concat(SqlUtils.VERTICAL_BAR)
                .concat(SqlUtils.AT)
                .concat(rp.getPartnerUrlCode())
                .concat(SqlUtils.AT)
                .concat(SqlUtils.VERTICAL_BAR)
                .concat(rp.getPeriodUrlCode())
                .concat(SqlUtils.VERTICAL_BAR)
                .concat(rp.getCommodityGroupTypeUrlCode())
                .concat(SqlUtils.VERTICAL_BAR)
                .concat(rp.getCommodityUrlCode())
                .concat(SqlUtils.VERTICAL_BAR);

        String hierGroupPartner = (new Integer(Constants.HIER_GROUP_PARTNER)).toString();
        inclTreePartnerInit = inclTreeStart
                .concat(Constants.FILTER_REQUEST_INIT)
                .concat(SqlUtils.SLASH)
                .concat(Constants.TREE_TEMPLATE_FULL)
                .concat(SqlUtils.SLASH)
                .concat(hierGroupPartner)
                .concat(SqlUtils.SLASH)
                .concat(urlTreePartner)
                .concat(SqlUtils.SLASH)
                .concat(rp.getPartnerIdsHS())
                .concat(SqlUtils.SLASH);
        inclTreePartnerChildren = inclTreeStart
                .concat(Constants.FILTER_REQUEST_CHILDREN)
                .concat(SqlUtils.SLASH)
                .concat(Constants.TREE_TEMPLATE_FULL)
                .concat(SqlUtils.SLASH)
                .concat(hierGroupPartner)
                .concat(SqlUtils.SLASH)
                .concat(urlTreePartner)
                .concat(SqlUtils.SLASH)
                .concat(Constants.TREE_VAR_PARENT_ID)
                .concat(SqlUtils.SLASH);
        inclTreePartnerSearch = inclTreeStart
                .concat(Constants.FILTER_REQUEST_SEARCH)
                .concat(SqlUtils.SLASH)
                .concat(Constants.TREE_TEMPLATE_FULL)
                .concat(SqlUtils.SLASH)
                .concat(hierGroupPartner)
                .concat(SqlUtils.SLASH)
                .concat(urlTreePartner)
                .concat(SqlUtils.SLASH)
                .concat(rp.getPartnerIdsHS())
                .concat(SqlUtils.SLASH)
                .concat(Constants.TREE_VAR_SEARCH_TEXT)
                .concat(SqlUtils.SLASH);

        // </editor-fold>
        // <editor-fold defaultstate="collapsed" desc="Period">
        urlTreePeriod = urlTreeStart
                .concat(rp.getReporterUrlCode())
                .concat(SqlUtils.VERTICAL_BAR)
                .concat(rp.getTradeFlowUrlCode())
                .concat(SqlUtils.VERTICAL_BAR)
                .concat(rp.getPartnerUrlCode())
                .concat(SqlUtils.VERTICAL_BAR)
                .concat(SqlUtils.AT)
                .concat(rp.getPeriodUrlCode())
                .concat(SqlUtils.AT)
                .concat(SqlUtils.VERTICAL_BAR)
                .concat(rp.getCommodityGroupTypeUrlCode())
                .concat(SqlUtils.VERTICAL_BAR)
                .concat(rp.getCommodityUrlCode())
                .concat(SqlUtils.VERTICAL_BAR);

        String hierGroupPeriod = (new Integer(Constants.HIER_GROUP_PERIOD)).toString();
        inclTreePeriodInit = inclTreeStart
                .concat(Constants.FILTER_REQUEST_INIT)
                .concat(SqlUtils.SLASH)
                .concat(Constants.TREE_TEMPLATE_FULL)
                .concat(SqlUtils.SLASH)
                .concat(hierGroupPeriod)
                .concat(SqlUtils.SLASH)
                .concat(urlTreePeriod)
                .concat(SqlUtils.SLASH)
                .concat(rp.getPeriodIdsHS())
                .concat(SqlUtils.SLASH);
        inclTreePeriodChildren = inclTreeStart
                .concat(Constants.FILTER_REQUEST_CHILDREN)
                .concat(SqlUtils.SLASH)
                .concat(Constants.TREE_TEMPLATE_FULL)
                .concat(SqlUtils.SLASH)
                .concat(hierGroupPeriod)
                .concat(SqlUtils.SLASH)
                .concat(urlTreePeriod)
                .concat(SqlUtils.SLASH)
                .concat(Constants.TREE_VAR_PARENT_ID)
                .concat(SqlUtils.SLASH);
        inclTreePeriodSearch = inclTreeStart
                .concat(Constants.FILTER_REQUEST_SEARCH)
                .concat(SqlUtils.SLASH)
                .concat(Constants.TREE_TEMPLATE_FULL)
                .concat(SqlUtils.SLASH)
                .concat(hierGroupPeriod)
                .concat(SqlUtils.SLASH)
                .concat(urlTreePeriod)
                .concat(SqlUtils.SLASH)
                .concat(rp.getPeriodIdsHS())
                .concat(SqlUtils.SLASH)
                .concat(Constants.TREE_VAR_SEARCH_TEXT)
                .concat(SqlUtils.SLASH);

        // </editor-fold>
        // <editor-fold defaultstate="collapsed" desc="Classification">
        urlTreeClassification = urlTreeStart
                .concat(rp.getReporterUrlCode())
                .concat(SqlUtils.VERTICAL_BAR)
                .concat(rp.getTradeFlowUrlCode())
                .concat(SqlUtils.VERTICAL_BAR)
                .concat(rp.getPartnerUrlCode())
                .concat(SqlUtils.VERTICAL_BAR)
                .concat(rp.getPeriodUrlCode())
                .concat(SqlUtils.VERTICAL_BAR)
                .concat(SqlUtils.AT)
                .concat(SqlUtils.EMPTY)
                .concat(SqlUtils.AT)
                .concat(SqlUtils.VERTICAL_BAR);

        String hierGroupClassification = (new Integer(Constants.HIER_GROUP_COMMODITY_CLASSIFICATION)).toString();
        inclTreeClassificationInit = inclTreeStart
                .concat(Constants.FILTER_REQUEST_INIT)
                .concat(SqlUtils.SLASH)
                .concat(Constants.TREE_TEMPLATE_FULL)
                .concat(SqlUtils.SLASH)
                .concat(hierGroupClassification)
                .concat(SqlUtils.SLASH)
                .concat(urlTreeClassification)
                .concat(SqlUtils.SLASH)
                .concat(rp.getCommodityGroupTypeIdsHS())
                .concat(SqlUtils.SLASH);
        inclTreeClassificationChildren = inclTreeStart
                .concat(Constants.FILTER_REQUEST_CHILDREN)
                .concat(SqlUtils.SLASH)
                .concat(Constants.TREE_TEMPLATE_FULL)
                .concat(SqlUtils.SLASH)
                .concat(hierGroupClassification)
                .concat(SqlUtils.SLASH)
                .concat(urlTreeClassification)
                .concat(SqlUtils.SLASH)
                .concat(Constants.TREE_VAR_PARENT_ID)
                .concat(SqlUtils.SLASH);
        inclTreeClassificationSearch = inclTreeStart
                .concat(Constants.FILTER_REQUEST_SEARCH)
                .concat(SqlUtils.SLASH)
                .concat(Constants.TREE_TEMPLATE_FULL)
                .concat(SqlUtils.SLASH)
                .concat(hierGroupClassification)
                .concat(SqlUtils.SLASH)
                .concat(urlTreeClassification)
                .concat(SqlUtils.SLASH)
                .concat(rp.getCommodityGroupTypeIdsHS())
                .concat(SqlUtils.SLASH)
                .concat(Constants.TREE_VAR_SEARCH_TEXT)
                .concat(SqlUtils.SLASH);

        // </editor-fold>
        // <editor-fold defaultstate="collapsed" desc="Commodity">
        urlTreeCommodity = urlTreeStart
                .concat(rp.getReporterUrlCode())
                .concat(SqlUtils.VERTICAL_BAR)
                .concat(rp.getTradeFlowUrlCode())
                .concat(SqlUtils.VERTICAL_BAR)
                .concat(rp.getPartnerUrlCode())
                .concat(SqlUtils.VERTICAL_BAR)
                .concat(rp.getPeriodUrlCode())
                .concat(SqlUtils.VERTICAL_BAR)
                .concat(rp.getCommodityGroupTypeUrlCode())
                .concat(SqlUtils.VERTICAL_BAR)
                .concat(SqlUtils.AT)
                .concat(rp.getCommodityUrlCode())
                .concat(SqlUtils.AT)
                .concat(SqlUtils.VERTICAL_BAR);

        String hierGroupCommodity = (new Integer(Constants.HIER_GROUP_COMMODITY)).toString();
        inclTreeCommodityInit = inclTreeStart
                .concat(Constants.FILTER_REQUEST_INIT)
                .concat(SqlUtils.SLASH)
                .concat(Constants.TREE_TEMPLATE_FULL)
                .concat(SqlUtils.SLASH)
                .concat(hierGroupCommodity)
                .concat(SqlUtils.SLASH)
                .concat(urlTreeCommodity)
                .concat(SqlUtils.SLASH)
                .concat(rp.getCommodityIdsHS())
                .concat(SqlUtils.SLASH);
        inclTreeCommodityChildren = inclTreeStart
                .concat(Constants.FILTER_REQUEST_CHILDREN)
                .concat(SqlUtils.SLASH)
                .concat(Constants.TREE_TEMPLATE_FULL)
                .concat(SqlUtils.SLASH)
                .concat(hierGroupCommodity)
                .concat(SqlUtils.SLASH)
                .concat(urlTreeCommodity)
                .concat(SqlUtils.SLASH)
                .concat(Constants.TREE_VAR_PARENT_ID)
                .concat(SqlUtils.SLASH);
        inclTreeCommoditySearch = inclTreeStart
                .concat(Constants.FILTER_REQUEST_SEARCH)
                .concat(SqlUtils.SLASH)
                .concat(Constants.TREE_TEMPLATE_FULL)
                .concat(SqlUtils.SLASH)
                .concat(hierGroupCommodity)
                .concat(SqlUtils.SLASH)
                .concat(urlTreeCommodity)
                .concat(SqlUtils.SLASH)
                .concat(rp.getCommodityIdsHS())
                .concat(SqlUtils.SLASH)
                .concat(Constants.TREE_VAR_SEARCH_TEXT)
                .concat(SqlUtils.SLASH);

        // </editor-fold>
        // </editor-fold>
        urlTable = rp.getHttpHeader()
                .concat(SqlUtils.SLASH)
                .concat(rp.getLocaleUrlCode())
                .concat(SqlUtils.SLASH)
                .concat(Constants.SECTION_TRADE_TABLE)
                .concat(SqlUtils.SLASH)
                .concat(rp.getReporterUrlCode())
                .concat(SqlUtils.SLASH)
                .concat(rp.getTradeFlowUrlCode())
                .concat(SqlUtils.SLASH)
                .concat(rp.getPartnerUrlCode())
                .concat(SqlUtils.SLASH)
                .concat(rp.getPeriodUrlCode())
                .concat(SqlUtils.SLASH)
                .concat(rp.getCommodityGroupTypeUrlCode())
                .concat(SqlUtils.SLASH)
                .concat(rp.getCommodityUrlCode())
                .concat(SqlUtils.SLASH);

        topPartnersRank = rp.getPartnerRank();
        topPartners = new ArrayList<>();
        topPartners.add(5);
        topPartners.add(10);
        topPartners.add(15);
        topPartners.add(20);
        topPartners.add(25);
        top = Labels.get_UI_TITLE_TOP(rp.getLocale());
        partnersGenetivus = Labels.get_UI_TITLE_PARTNERS_GENETIVUS(rp.getLocale());
    }

    public String getLocaleUrlCode() {
        return rp.getLocaleUrlCode();
    }

    static String getTitle(String tradeFlowNames, String reporterName, String partnerNames) {
        StringBuilder buf = new StringBuilder();

        buf
                .append(tradeFlowNames);
        if (!SqlUtils.EMPTY.equals(reporterName)) {
            buf
                    .append(SqlUtils.COLON)
                    .append(SqlUtils.SPACE)
                    .append(reporterName);
        }
        if (!SqlUtils.EMPTY.equals(partnerNames)) {
            buf
                    .append(SqlUtils.SPACE)
                    .append(SqlUtils.HYPHEN)
                    .append(SqlUtils.SPACE)
                    .append(partnerNames);
        }

        return buf.toString();
    }

    public String getPageTitle() {
        return SqlUtils.EMPTY
                .concat(dvs.getAllNames(Queries.TradeFlowDimensionValue, rp.getTradeFlowIds(), rp.getLocale(), SqlUtils.COMMA.concat(SqlUtils.SPACE)))
                .concat(SqlUtils.COLON)
                .concat(SqlUtils.SPACE)
                .concat(dvs.getAllNames(Queries.CountryDimensionValue, rp.getReporterIds(), rp.getLocale(), SqlUtils.COMMA.concat(SqlUtils.SPACE)))
                .concat(SqlUtils.SPACE)
                .concat(SqlUtils.HYPHEN)
                .concat(SqlUtils.SPACE)
                .concat(dvs.getAllNames(Queries.CountryDimensionValue, rp.getPartnerIds(), rp.getLocale(), SqlUtils.COMMA.concat(SqlUtils.SPACE)))
                .concat(SqlUtils.SPACE).concat(SqlUtils.VERTICAL_BAR).concat(SqlUtils.SPACE)
                .concat(Labels.get_UI_TITLE_MAIN_SHORT(rp.getLocale()));
    }

    public String getPageTitleShort() {
        return SqlUtils.EMPTY
                .concat(dvs.getAllNames(Queries.TradeFlowDimensionValue, rp.getTradeFlowIds(), rp.getLocale(), SqlUtils.COMMA.concat(SqlUtils.SPACE)))
                .concat(SqlUtils.COMMA)
                .concat(SqlUtils.SPACE)
                .concat(Labels.get_UI_VALUE_UOM_USD(rp.getLocale()))
                .concat(SqlUtils.COLON)
                .concat(SqlUtils.SPACE)
                .concat(dvs.getAllNames(Queries.CountryDimensionValue, rp.getReporterIds(), rp.getLocale(), SqlUtils.COMMA.concat(SqlUtils.SPACE)))
                .concat(SqlUtils.SPACE)
                .concat(SqlUtils.HYPHEN)
                .concat(SqlUtils.SPACE)
                .concat(dvs.getAllNames(Queries.CountryDimensionValue, rp.getPartnerIds(), rp.getLocale(), SqlUtils.COMMA.concat(SqlUtils.SPACE)));
    }

    public String getUI_FOOTER_TRADE() {
        return Labels.get_UI_FOOTER_TRADE(rp.getLocale());
    }

    public String getUI_TITLE_REPORTERS() {
        return Labels.get_UI_TITLE_REPORTER(rp.getLocale());
    }

    public String getUI_TITLE_TRADE_FLOW() {
        return Labels.get_UI_TITLE_TRADE_FLOW(rp.getLocale());
    }

    public String getUI_TITLE_PARTNER() {
        return Labels.get_UI_TITLE_PARTNER(rp.getLocale());
    }

    public String getUI_TITLE_PERIOD() {
        return Labels.get_UI_TITLE_PERIOD(rp.getLocale());
    }

    public String getUI_TITLE_COMMODITY_CLASSIFICATION() {
        return Labels.get_UI_TITLE_COMMODITY_CLASSIFICATION(rp.getLocale());
    }

    public String getUI_TITLE_COMMODITY() {
        return Labels.get_UI_TITLE_COMMODITY(rp.getLocale());
    }

    public List<DimensionValue> getReporters() {
        return reporters;
    }

    public List<DimensionValue> getTradeFlows() {
        return tradeFlows;
    }

    public List<DimensionValue> getPartners() {
        return partners;
    }

    public List<DimensionValue> getPeriods() {
        return periods;
    }

    public List<DimensionValue> getClassifications() {
        return classifications;
    }

    public List<DimensionValue> getCommodities() {
        return commodities;
    }

    public String getInclTreeReporterInit() {
        return inclTreeReporterInit;
    }

    public String getInclTreeReporterChildren() {
        return inclTreeReporterChildren;
    }

    public String getInclTreeReporterSearch() {
        return inclTreeReporterSearch;
    }

    public String getInclTreeTradeFlowInit() {
        return inclTreeTradeFlowInit;
    }

    public String getInclTreeTradeFlowChildren() {
        return inclTreeTradeFlowChildren;
    }

    public String getInclTreeTradeFlowSearch() {
        return inclTreeTradeFlowSearch;
    }

    public String getInclTreePartnerInit() {
        return inclTreePartnerInit;
    }

    public String getInclTreePartnerChildren() {
        return inclTreePartnerChildren;
    }

    public String getInclTreePartnerSearch() {
        return inclTreePartnerSearch;
    }

    public String getInclTreePeriodInit() {
        return inclTreePeriodInit;
    }

    public String getInclTreePeriodChildren() {
        return inclTreePeriodChildren;
    }

    public String getInclTreePeriodSearch() {
        return inclTreePeriodSearch;
    }

    public String getInclTreeClassificationInit() {
        return inclTreeClassificationInit;
    }

    public String getInclTreeClassificationChildren() {
        return inclTreeClassificationChildren;
    }

    public String getInclTreeClassificationSearch() {
        return inclTreeClassificationSearch;
    }

    public String getInclTreeCommodityInit() {
        return inclTreeCommodityInit;
    }

    public String getInclTreeCommodityChildren() {
        return inclTreeCommodityChildren;
    }

    public String getInclTreeCommoditySearch() {
        return inclTreeCommoditySearch;
    }

    public String getTradeSummaryUrl() {
        return rp.getContextPath()
                .concat(SqlUtils.SLASH)
                .concat(rp.getLocaleUrlCode())
                .concat(SqlUtils.SLASH)
                .concat(Constants.SECTION_TRADE_SUMMARY)
                .concat(SqlUtils.SLASH)
                .concat(rp.getCommodityGroupTypeUrlCode())
                .concat(SqlUtils.SLASH)
                .concat(rp.getReporterUrlCode())
                .concat(SqlUtils.OCTOTHORPE)
                .concat(rp.getReporterUrlCode());
    }

    public String getTableUrl() {
        return urlTable;
    }

    public String getDynamicJsUrl() {
        return rp.getContextPath()
                .concat(SqlUtils.SLASH)
                .concat(rp.getLocaleUrlCode())
                .concat(SqlUtils.SLASH)
                .concat(Constants.SECTION_TRADE_JS)
                .concat(SqlUtils.SLASH)
                .concat(rp.getReporterUrlCode())
                .concat(SqlUtils.SLASH)
                .concat(rp.getTradeFlowUrlCode())
                .concat(SqlUtils.SLASH)
                .concat(rp.getPartnerUrlCode())
                .concat(SqlUtils.SLASH)
                .concat(rp.getPeriodUrlCode())
                .concat(SqlUtils.SLASH)
                .concat(rp.getCommodityGroupTypeUrlCode())
                .concat(SqlUtils.SLASH)
                .concat(rp.getCommodityUrlCode());
    }

    public String getSelectedPartnerUrl(String urlCode) {
        String url = SqlUtils.excludeUrlCode(urlCode, rp.getPartnerUrlCode());
        return url.isEmpty() ? SqlUtils.EMPTY
                : rp.getContextPath()
                .concat(SqlUtils.SLASH)
                .concat(rp.getLocaleUrlCode())
                .concat(SqlUtils.SLASH)
                .concat(Constants.SECTION_TRADE)
                .concat(SqlUtils.SLASH)
                .concat(rp.getReporterUrlCode())
                .concat(SqlUtils.SLASH)
                .concat(rp.getTradeFlowUrlCode())
                .concat(SqlUtils.SLASH)
                .concat(url)
                .concat(SqlUtils.SLASH)
                .concat(rp.getPeriodUrlCode())
                .concat(SqlUtils.SLASH)
                .concat(rp.getCommodityGroupTypeUrlCode())
                .concat(SqlUtils.SLASH)
                .concat(rp.getCommodityUrlCode());
    }

    public String getSelectedPeriodUrl(String urlCode) {
        String url = SqlUtils.excludeUrlCode(urlCode, rp.getPeriodUrlCode());
        return url.isEmpty() ? SqlUtils.EMPTY
                : rp.getContextPath()
                .concat(SqlUtils.SLASH)
                .concat(rp.getLocaleUrlCode())
                .concat(SqlUtils.SLASH)
                .concat(Constants.SECTION_TRADE)
                .concat(SqlUtils.SLASH)
                .concat(rp.getReporterUrlCode())
                .concat(SqlUtils.SLASH)
                .concat(rp.getTradeFlowUrlCode())
                .concat(SqlUtils.SLASH)
                .concat(rp.getPartnerUrlCode())
                .concat(SqlUtils.SLASH)
                .concat(url)
                .concat(SqlUtils.SLASH)
                .concat(rp.getCommodityGroupTypeUrlCode())
                .concat(SqlUtils.SLASH)
                .concat(rp.getCommodityUrlCode());
    }

    public String getSelectedCommodityUrl(String urlCode) {
        String url = SqlUtils.excludeUrlCode(urlCode, rp.getCommodityUrlCode());
        return url.isEmpty() ? SqlUtils.EMPTY
                : rp.getContextPath()
                .concat(SqlUtils.SLASH)
                .concat(rp.getLocaleUrlCode())
                .concat(SqlUtils.SLASH)
                .concat(Constants.SECTION_TRADE)
                .concat(SqlUtils.SLASH)
                .concat(rp.getReporterUrlCode())
                .concat(SqlUtils.SLASH)
                .concat(rp.getTradeFlowUrlCode())
                .concat(SqlUtils.SLASH)
                .concat(rp.getPartnerUrlCode())
                .concat(SqlUtils.SLASH)
                .concat(rp.getPeriodUrlCode())
                .concat(SqlUtils.SLASH)
                .concat(rp.getCommodityGroupTypeUrlCode())
                .concat(SqlUtils.SLASH)
                .concat(url);
    }

    public String getTop() {
        return top;
    }

    public String getPartnersGenetivus() {
        return partnersGenetivus;
    }

    public ArrayList<Integer> getTopPartners() {
        return topPartners;
    }

    public String getTradeSummaryShortTitle() {
        return TradeSummaryBean.getTitle(false, rp.getLocale(),
                dvs.getAllNames(Queries.CountryDimensionValue, rp.getReporterIds(), rp.getLocale()));
    }

    public int getTopPartnersRank() {
        return topPartnersRank;
    }

    public String getUI_TITLE_SOURCES() {
        return Labels.get_UI_TITLE_SOURCES(rp.getLocale());
    }

    public String getHttpHeader() {
        return rp.getHttpHeader();
    }

    public String getKeyWords() {
        return Labels.get_META_KEY_WORDS_TRADE(rp.getLocale())
                .concat(SqlUtils.COMMA).concat(SqlUtils.SPACE)
                .concat(reporterNames)
                .concat(SqlUtils.COMMA).concat(SqlUtils.SPACE)
                .concat(periodNames)
                .concat(SqlUtils.COMMA).concat(SqlUtils.SPACE)
                .concat(partnerNames)
                .concat(SqlUtils.COMMA).concat(SqlUtils.SPACE)
                .concat(classificationNames)
                .concat(SqlUtils.COMMA).concat(SqlUtils.SPACE)
                .concat(commodityNames);
    }

    public String getDescription() {
        String r = SqlUtils.EMPTY;
        for (DimensionValue tradeFlow : tradeFlows) {
            if (Objects.equals(tradeFlow.getId(), Constants.DIM_TRADE_FLOW_EXPORT_ID)) {
                r = r
                        .concat(tradeFlow.getName())
                        .concat(SqlUtils.SPACE)
                        .concat(Labels.get_WORD_OF_PRODUCTS(rp.getLocale()))
                        .concat(SqlUtils.SPACE)
                        .concat(Labels.get_WORD_FROM(rp.getLocale()))
                        .concat(SqlUtils.SPACE)
                        .concat(reporterNamesGen)
                        .concat(SqlUtils.SPACE)
                        .concat(Labels.get_WORD_TO(rp.getLocale()))
                        .concat(SqlUtils.SPACE)
                        .concat(partnerNamesAcc)
                        .concat(SqlUtils.SPACE)
                        .concat(Labels.get_WORD_IN(rp.getLocale()))
                        .concat(SqlUtils.SPACE)
                        .concat(periodNames)
                        .concat(SqlUtils.SPACE);
            } else if (Objects.equals(tradeFlow.getId(), Constants.DIM_TRADE_FLOW_IMPORT_ID)) {
                r = r
                        .concat(SqlUtils.SPACE)
                        .concat(tradeFlow.getName())
                        .concat(SqlUtils.SPACE)
                        .concat(Labels.get_WORD_OF_PRODUCTS(rp.getLocale()))
                        .concat(SqlUtils.SPACE)
                        .concat(Labels.get_WORD_FROM(rp.getLocale()))
                        .concat(SqlUtils.SPACE)
                        .concat(partnerNamesGen)
                        .concat(SqlUtils.SPACE)
                        .concat(Labels.get_WORD_TO(rp.getLocale()))
                        .concat(SqlUtils.SPACE)
                        .concat(reporterNamesAcc)
                        .concat(SqlUtils.SPACE)
                        .concat(Labels.get_WORD_IN(rp.getLocale()))
                        .concat(SqlUtils.SPACE)
                        .concat(periodNames)
                        .concat(SqlUtils.SPACE);
            }
        }
        r = r
                .concat(SqlUtils.BRACKET_LEFT)
                .concat(classificationNames)
                .concat(SqlUtils.COLON)
                .concat(SqlUtils.SPACE)
                .concat(commodityNames)
                .concat(SqlUtils.BRACKET_RIGHT);
        return r;
    }

    static String getDescription(
            Locale locale,
            List<Integer> tradeFlowIds,
            List<String> tradeFlowNames,
            String periodNames,
            String reporterNamesGen,
            String reporterNamesAcc,
            String partnerNamesGen,
            String partnerNamesAcc,
            String classificationName,
            String commodityNames
    ) {
        StringBuilder buf = new StringBuilder();
        for (int i = 0; i < tradeFlowIds.size(); i++) {
            Integer tradeFlowId = tradeFlowIds.get(i);
            String tradeFlowName = tradeFlowNames.get(i);
            if (Objects.equals(tradeFlowId, Constants.DIM_TRADE_FLOW_EXPORT_ID)) {
                buf
                        .append(tradeFlowName)
                        .append(SqlUtils.SPACE)
                        .append(Labels.get_WORD_OF_PRODUCTS(locale));
                if (!SqlUtils.EMPTY.equals(reporterNamesGen)) {
                    buf
                            .append(SqlUtils.SPACE)
                            .append(Labels.get_WORD_FROM(locale))
                            .append(SqlUtils.SPACE)
                            .append(reporterNamesGen);
                }
                if (!SqlUtils.EMPTY.equals(partnerNamesAcc)) {
                    buf
                            .append(SqlUtils.SPACE)
                            .append(Labels.get_WORD_TO(locale))
                            .append(SqlUtils.SPACE)
                            .append(partnerNamesAcc);
                }
                if (!SqlUtils.EMPTY.equals(periodNames)) {
                    buf
                            .append(SqlUtils.SPACE)
                            .append(Labels.get_WORD_IN(locale))
                            .append(SqlUtils.SPACE)
                            .append(periodNames);
                }
                buf
                        .append(SqlUtils.SPACE);
            } else if (Objects.equals(tradeFlowId, Constants.DIM_TRADE_FLOW_IMPORT_ID)) {
                buf
                        .append(SqlUtils.SPACE)
                        .append(tradeFlowName)
                        .append(SqlUtils.SPACE)
                        .append(Labels.get_WORD_OF_PRODUCTS(locale));
                if (!SqlUtils.EMPTY.equals(partnerNamesGen)) {
                    buf
                            .append(SqlUtils.SPACE)
                            .append(Labels.get_WORD_FROM(locale))
                            .append(SqlUtils.SPACE)
                            .append(partnerNamesGen);
                }
                if (!SqlUtils.EMPTY.equals(reporterNamesAcc)) {
                    buf
                            .append(SqlUtils.SPACE)
                            .append(Labels.get_WORD_TO(locale))
                            .append(SqlUtils.SPACE)
                            .append(reporterNamesAcc);
                }
                if (!SqlUtils.EMPTY.equals(periodNames)) {
                    buf
                            .append(SqlUtils.SPACE)
                            .append(Labels.get_WORD_IN(locale))
                            .append(SqlUtils.SPACE)
                            .append(periodNames);
                }
                buf
                        .append(SqlUtils.SPACE);
            }
        }
        if (!SqlUtils.EMPTY.equals(classificationName) || !SqlUtils.EMPTY.equals(commodityNames)) {
            buf
                    .append(SqlUtils.BRACKET_LEFT)
                    .append(classificationName)
                    .append(SqlUtils.SPACE)
                    .append(commodityNames)
                    .append(SqlUtils.BRACKET_RIGHT);
        }
        return buf.toString();
    }

    static String getUrl(
            String contextPath,
            String localeUrlCode,
            String reporterUrlCode,
            String tradeFlowUrlCode,
            String partnerUrlCode,
            String periodUrlCode,
            String classificationUrlCode,
            String commodityUrlCode
    ) {
        StringBuilder buf = new StringBuilder();
        buf
                .append(contextPath)
                .append(SqlUtils.SLASH)
                .append(localeUrlCode)
                .append(SqlUtils.SLASH)
                .append(Constants.SECTION_TRADE)
                .append(SqlUtils.SLASH)
                .append(reporterUrlCode)
                .append(SqlUtils.SLASH)
                .append(tradeFlowUrlCode)
                .append(SqlUtils.SLASH)
                .append(partnerUrlCode)
                .append(SqlUtils.SLASH)
                .append(periodUrlCode)
                .append(SqlUtils.SLASH)
                .append(classificationUrlCode)
                .append(SqlUtils.SLASH)
                .append(commodityUrlCode);
        return buf.toString();
    }
    
    
    public String getPARAMETER_LOCALE() {
        return Parameters.LOCALE;
    }

    public String getPARAMETER_SECTION() {
        return Parameters.SECTION;
    }

    public String getPARAMETER_REPORTER() {
        return Parameters.REPORTER;
    }

    public String getPARAMETER_TRADE_FLOW() {
        return Parameters.TRADE_FLOW;
    }

    public String getPARAMETER_PARTNER() {
        return Parameters.PARTNER;
    }

    public String getPARAMETER_PERIOD() {
        return Parameters.PERIOD;
    }

    public String getPARAMETER_COMMODITY_GROUP_TYPE() {
        return Parameters.COMMODITY_GROUP_TYPE;
    }

    public String getPARAMETER_COMMODITY() {
        return Parameters.COMMODITY;
    }
    
    public String getSectionUrlCode() {
        return rp.getSection();
    }
    
    public String getReporterUrlCode() {
        return rp.getReporterUrlCode();
    }
    
    public String getTradeFlowUrlCode() {
        return rp.getTradeFlowUrlCode();
    }
    
    public String getPartnerUrlCode() {
        return rp.getPartnerUrlCode();
    }
    
    public String getPeriodUrlCode() {
        return rp.getPeriodUrlCode();
    }
    
    public String getCommodityGroupTypeUrlCode() {
        return rp.getCommodityGroupTypeUrlCode();
    }
    
    public String getCommodityUrlCode() {
        return rp.getCommodityUrlCode();
    }
    
}
